<?php
namespace Home\Controller;
use Think\Controller;
class AlbumModelController extends Controller {

	private $m='';
	public function _initialize(){
		if($this->m==''){
			$this->m = M('Albums');
		}
	}

	//获取相册信息
	public function get_info($aids){
		return $this->m->select($aids);
	}	

	//删除多个相册
	public function album_dels(){
		$aids = I('post.aids');
		if(!$aids){
			exit;
		}
		$st = $this->m->delete($aids);
		if($st > 0){
			echo 'ok';
			$cls_Imgs = new ImgsModelController();
			foreach(explode(',',$aids) as $v){
				$path = C('album_img_dir').$v;
				del_dir($path);
				$cls_Imgs->del_album_pics($v);
			}
		}
	}

	//删除单个相册
	public function album_del(){
		$aid = I('post.aid');
		if(!$aid){
			exit('err');		
		}
		$st = $this->m->delete($aid);
		if($st > 0){
			$path = C('album_img_dir').$aid;
			del_dir($path);
			$cls_Imgs = new ImgsModelController();
			$cls_Imgs->del_album_pics($aid);
			echo 'ok';
		}else{
			echo 'err';
		}
	}


	//获取全部相册列表 
	public function get_album_all(){
		if(I('get.keyword')){
			$keyword = I('get.keyword');
            $where['album_name'] = array('like',"%{$keyword}%");
        }else{
        	$where = '';
        }
		//return Get_Fpage($this->m,$fields='',$where,$order = 'album_id desc',$pcount = 10);
		$data = Get_Fpage($this->m,$fields='',$where,$order = 'album_id desc',$pcount = 10);
		if(count($data['list']) > 0){
			return $data;
		}else{
			return 'err';
		}
	}



	//搜索相册
	public function get_album_search($keyword){
		$where['album_name'] = array('like',"%{$keyword}%");
		return Get_Fpage($this->m,$fields='',$where,$order = '',$pcount = 10);
	}



	//相册的添加
	public function album_add(){
		$album_name = I('post.album_name');
		if(!$album_name){
			exit('e1');		//相册名不得为空！
		}
		$st = $this->m->where(array('album_name'=>$album_name))->find();
		if($st){
			exit('e2');		//相册以存在！
		}
		$data['album_name'] = $album_name;
		$data['album_ctime'] = time();
		$data['album_flower'] = 0;
		$data['album_collect'] = 0;
		$data['album_banner'] = 'banner.jpg';
		if($this->m->create($data)){
			$st = $this->m->add();
			if($st > 0){
				$path = C('album_img_dir').$st;
				if(!is_dir($path)){
					mkdir($path,0777,true);
				}
				echo 'ok';
			}
		}else{
			echo 'e3';	//添加出错！
		}
	}

	//修改相册名
	public function album_edit(){
		$album_name = I('post.album_name');
		$album_id = I('post.album_id');
		if(!$album_name || !$album_id){
			exit('e1');	//数据不得为空！
		}
		$data['album_id']=$album_id;
		$data['album_name']=$album_name;
		$st = $this->m->data($data)->save();
		if($st > 0){
			echo 'ok';
		}
	}


	//修改相册信息
	public function album_edits($arr='',$stat=""){
		if($arr){
			$data = $arr;
		}else{
			$data = I('post.');
		}
		if($this->m->create($data)){
			$st = $this->m->save();
			if($st>0){
				if(!$stat){
					echo 'ok';
				}
			}
		}
	}


	//获取指定相册信息
	public function get_album_info($aid,$field){
		return $this->m->field($field)->where(array('album_id'=>$aid))->find();
	}
}